﻿using System;
using System.Globalization;

namespace BToolkit
{
    public class TimeUtils
    {
        public const string FormatNormal = "yyyy-MM-dd HH:mm:ss";
        public const string FormatYMD = "yyyy-MM-dd";
        public const string FormatHM = "HH:mm";
        public const string FormatMS = "mm:ss";
        public const string FormatHMS = "HH:mm:ss";
        public const string FormatNoSpace = "yyyyMMddHHmmss";

        /// <summary>
        /// DateTime转字符串
        /// </summary>
        public static DateTime StringToDateTime(string time)
        {
            try
            {
                return DateTime.Parse(time);
            }catch(Exception e)
            {
                Debuger.LogError(e);
            }
            return default;
        }

        /// <summary>
        /// DateTime转字符串
        /// </summary>
        public static DateTime StringToDateTime(string time, string format)
        {
            DateTimeFormatInfo dtFormat = new DateTimeFormatInfo();
            dtFormat.ShortDatePattern = format;
            return Convert.ToDateTime(time, dtFormat);
        }

        /// <summary>
        /// DateTime转时间戳(秒)
        /// </summary>
        public static long StringToTimestamp(string time)
        {
            DateTime dateTime = StringToDateTime(time);
            return DateTimeToTimestamp(dateTime);
        }

        /// <summary>
        /// DateTime转时间戳(秒)
        /// </summary>
        public static long StringToTimestamp(string time, string format)
        {
            DateTime dateTime = StringToDateTime(time, format);
            return DateTimeToTimestamp(dateTime);
        }

        /// <summary>
        /// 字符串转DateTime
        /// </summary>
        public static string DateTimeToString(DateTime time, string format = FormatNormal)
        {
            return time.ToString(format);
        }

        /// <summary>
        /// 获取两个时间的相隔天数
        /// </summary>
        public static int GetSubDays(DateTime timeNew, DateTime timePast)
        {
            return timeNew.Subtract(timePast).Days;
        }

        /// <summary>
        /// 比较两个时间的大小
        /// </summary>
        public static bool CompareTime(DateTime bigerTime, DateTime smallerTime)
        {
            return bigerTime.Ticks - smallerTime.Ticks > 0;
        }

        /// <summary>
        /// 获取当前时间戳（秒）
        /// </summary>
        public static long GetCurrTimeStamp()
        {
            //柏林时间DateTime.UtcNow
            TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds);
        }

        /// <summary>
        /// DateTime转时间戳(秒)
        /// </summary>
        public static int DateTimeToTimestamp(DateTime time)
        {
            DateTime startTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt32((time - startTime).TotalSeconds);
        }

        /// <summary>
        /// 时间戳换DateTime
        /// </summary>
        public static DateTime TimestampToDateTime(long seconds)
        {
            DateTime time = DateTime.MinValue;
            DateTime startTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            time = startTime.AddSeconds(seconds);
            return time;
        }

        /// <summary>
        /// 时间戳换DateTime
        /// </summary>
        public static DateTime TimeSpanToDateTime(TimeSpan timeSpan)
        {
            return TimestampToDateTime(Convert.ToInt64(timeSpan.TotalSeconds));
        }
    }
}